package logic

import (
	"context"
	"fmt"
	"good_srv/good"
	"good_srv/internal/svc"
	"good_srv/models"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册接口
func (l *RegisterLogic) Register(in *good.RegisterRequest) (*good.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	users := models.User{
		Phone: in.Phone,
	}
	models.DB.Where("phone=?", in.Phone).Find(&users)
	if users.ID != 0 {
		logx.Error("手机号码已重复使用")
		return nil, fmt.Errorf("手机号码已重复使用")
	}
	models.DB.Create(&users)
	return &good.RegisterResponse{
		Id: int64(users.ID),
	}, nil
}
